<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="选择销售单"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <div>
      <sales ref="sales"></sales>
    </div>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="onSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
<script>
import VolBox from "@/components/basic/VolBox.vue";
import sales from "@/views/salesinfo/sales/q_sales.vue";
//这里使用的vue2语法，也可以写成vue3语法
export default {
  components: {
    "vol-box": VolBox,
    sales,
  },
  data() {
    return {
      model: false,
    };
  },
  methods: {
    async onModelClose() {
      this.$refs.sales.searchFormFields.AuditStatus = "1";
      await this.$refs.sales.$refs.grid.search();
    },
    open(val) {
      this.model = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.sales) {
        
          clearInterval(setIntervalTiem);
          if(this.$route.path == "/q_after_delivery"){
            this.$refs.sales.searchFormFields.Zhibao_state = "1";
            this.$refs.sales.searchFormFields.CreateDate = [,];
          }
          if(this.$route.path == "/q_sales_return"){
            this.$refs.sales.searchFormFields.WHSE_no =val;
            this.$refs.sales.searchFormFields.AuditStatus = "1";
            this.$refs.sales.searchFormFields.CreateDate = [,];
            this.$refs.sales.searchFormOptions.forEach(item => {
                item.forEach(x => {
                  if(x.field == "WHSE_no"){
                    x.disabled = true
                  }
                })
             })
          }
          if(this.$route.path == "/q_install_request"){
            this.$refs.sales.searchFormFields.Install_status = "0";
            this.$refs.sales.searchFormFields.CreateDate = [,];
          }
          this.$refs.sales.$refs.grid.search();
        }
      }, 100);
    },

    onSelect() {
      let rowObj = this.$refs.sales.$refs.grid.getSelectRows();
      if (rowObj.length!=1) {
        this.base.ElMessageFn("请选择一条数据");
        return (this.model = true);
      }
      let AuditStatusSome = rowObj.some((item) => item.AuditStatus != 1);
      if (AuditStatusSome) {
        this.base.ElMessageFn("请勿选择审核未通过单据");
        return;
      }
      if(rowObj[0].Zhibao_state!=1&&this.$route.path == "/q_after_delivery"){
            this.base.ElMessageFn("请选择质保中单据")
             return ;
          }
          if(rowObj[0].Install_status!=0&&this.$route.path == "/q_install_request"){
            this.base.ElMessageFn("请选择未安装单据")
             return ;
          }
      this.$emit("parentCall", ($parent) => {
        if(this.$route.path == "/q_sales_return"){
            $parent.editFormFields.Sales_sub =
            rowObj[0].Voucher_sub;//编码
            $parent.editFormFields.Sales = rowObj[0].Sales;//ID
          }else if(this.$route.path == "/q_after_delivery"){
            $parent.editFormFields.Sales_sub =
            rowObj[0].Voucher_sub;//编码
            $parent.editFormFields.Sales = rowObj[0].Sales;//ID
            $parent.editFormFields.Client_name = rowObj[0].Client;//客户
            $parent.editFormFields.WHSE_no = rowObj[0].WHSE_no;//机构
          }else if(this.$route.path == "/q_install_request"){
            $parent.editFormFields.Sales =
            rowObj[0].Sales;
            $parent.editFormFields.Sales_sub =
            rowObj[0].Voucher_sub;
            $parent.editFormFields.Client =rowObj[0].Client
            $parent.editFormFields.Goods_num =rowObj[0].Goods_num
            //  $parent.editFormFields.Client =JSON.stringify(rowObj[0].Client);
            // $parent.editFormFields.Install_address =
            // rowObj[0].Delivery_address;
            // $parent.editFormFields.Install_date =
            // rowObj[0].Delivery_time;
            // 发送请求
            // try {
            //   this. http.post('api/q_sales/GetsalesList?fID='+rowObj[0].Sales, {}, "").then(reslut => {
            //   console.log("获取明细表数据", reslut)
            //     if (reslut.length) {
            //       this.$emit("parentCall", ($parent) => {
            //         $parent.$refs.detail.rowData.unshift(...reslut);
            //       })
            //     }         
            //   })
            // } catch (error) {
            //   console.log(error)
            // }
            
          }else{
            $parent.editFormFields.Voucher_sub_sales = rowObj[0].Voucher_sub;
          }
      });
      this.onModelClose();
      return (this.model = false);
    },
  },
};
</script>